/*
 * @Author: wangchao
 * @Date: 2024-10-04 12:07:03
 * @LastEditors: wangchao
 * @LastEditTime: 2024-10-11 14:44:25
 * @FilePath: \JD-RTT-Driver\applications\u_alarm.c
 * @Description:
 * Copyright (c) 2024 by Bingshan Guardian, All Rights Reserved.
 */

#include "u_alarm.h"
#include "u_params.h"
#include "u_utilits.h"
#include "u_app.h"
#include "u_data.h"

#define DBG_TAG "ALM"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

//
static BMS_L3_FAIL_INST APP_L3_FAIL_INST;
static BMS_L2_FAIL_INST APP_L2_FAIL_INST;
static BMS_L1_FAIL_INST APP_L1_FAIL_INST;
//
static BMS_L1_FAIL_UNION APP_L1_FAIL_REPORT;
static BMS_L2_FAIL_UNION APP_L2_FAIL_REPORT;
static BMS_L3_FAIL_UNION APP_L3_FAIL_REPORT;

// 故障消音按键
rt_bool_t STOP_WARNING_SOUND = RT_FALSE;

rt_bool_t bms_exception_occurred(void)
{
    // if (SYSTEM_FAULT_STOP_FLAG || SYSTEM_ALARM_STOP_FLAG)
    // {
    //     return RT_TRUE;
    // }
    return RT_FALSE;
}

static void APP_L1_Fail_Init(void)
{
    rt_int16_t t_threshold = 0;
    // Clear Alarm Words
    rt_memset(APP_L1_FAIL_REPORT.WORDS, 0, 6);

    // 子系统1正极泵电流过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.P1_Pump_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极泵电流过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.P2_Pump_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极泵电流过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.N1_Pump_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极泵电流过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.N2_Pump_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正极泵电流过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.P1_Pump_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极泵电流过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.P2_Pump_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极泵电流过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.N1_Pump_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极泵电流过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.N2_Pump_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.P1_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.P2_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.N1_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.N2_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.P1_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.P2_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.N1_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.N2_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1正极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.P1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.P2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.N1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.N2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.P1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.P2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.N1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.N2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1正极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.P1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.P2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.N1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.N2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.P1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.P2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.N1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.N2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1正负极流量差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.PN1_Flow_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正负极流量差过低
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.PN2_Flow_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正负极压力差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.PN1_Pressure_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正负极压力差过低
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.PN2_Pressure_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // A侧电堆电压高&低报警
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.A_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.A_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    // A侧电堆异常
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.A_Estack_Fault[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }

    // B侧电堆电压高&低报警
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.B_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.B_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    // B侧电堆异常
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.B_Estack_Fault[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }

    // 系统电流（充电）过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.Charge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 系统电流（放电）过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.DisCharge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // A SOC H
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.Charge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // A SOC L
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L1_FAIL_INST.DisCharge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // B SOC H
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.Charge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // B SOC L
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L1_FAIL_INST.DisCharge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
}

static void APP_L2_Fail_Init(void)
{
    rt_int16_t t_threshold = 0;
    // Clear Alarm Words
    rt_memset(APP_L2_FAIL_REPORT.WORDS, 0, 6);

    // 子系统1电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.P1_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.P2_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.N1_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.N2_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.P1_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.P2_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.N1_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.N2_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1正极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.P1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.P2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.N1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.N2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.P1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.P2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.N1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.N2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1正极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.P1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.P2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.N1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.N2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.P1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.P2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.N1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.N2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1正负极流量差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.PN1_Flow_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正负极流量差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.PN2_Flow_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正负极压力差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.PN1_Pressure_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正负极压力差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.PN2_Pressure_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // A侧电堆电压高&低报警
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.A_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.A_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    // A侧电堆异常
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.A_Estack_Fault[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }

    // B侧电堆电压高&低报警
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.B_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_LOW_POINT(&APP_L2_FAIL_INST.B_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    // B侧电堆异常
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.B_Estack_Fault[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }

    // 系统电流（充电）过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.Charge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 系统电流（放电）过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L2_FAIL_INST.DisCharge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
}

static void APP_L3_Fail_Init(void)
{
    rt_int16_t t_threshold = 0;
    // Clear Alarm Words
    rt_memset(APP_L3_FAIL_REPORT.WORDS, 0, 6);
    //
    // 子系统1电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.P1_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.P2_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.N1_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2电解液进液温度过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.N2_Electrolyte_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.P1_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.P2_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.N1_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2储罐温度过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.N2_StorageTank_Temp, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1正极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.P1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.P2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.N1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极电解液流量过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.N2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.P1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.P2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.N1_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极电解液流量过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.N2_Flow, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 子系统1正极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.P1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.P2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.N1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极系统压力过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.N2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.P1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.P2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1负极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.N1_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2负极系统压力过低
    t_threshold = 0;
    BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.N2_Pressure, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正负极流量差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.PN1_Flow_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统2正负极流量差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.PN2_Flow_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正负极压力差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.PN1_Pressure_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 子系统1正负极压力差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.PN2_Pressure_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // A侧电堆电压高&低报警
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.A_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.A_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    // A侧电堆异常
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.A_Estack_Fault[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }

    // B侧电堆电压高&低报警
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.B_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_LOW_POINT(&APP_L3_FAIL_INST.B_Estack_Volt[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }
    // B侧电堆异常
    t_threshold = 0;
    for (rt_uint8_t i = 0; i < 6; i++)
    {
        BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.B_Estack_Fault[i], RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    }

    // 系统电流（充电）过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.Charge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 系统电流（放电）过高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.DisCharge_Current, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);

    // 变频器P1频率高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.P1_Freq, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 变频器P2频率高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.P2_Freq, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 变频器N1频率高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.N1_Freq, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // 变频器N2频率高
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.N2_Freq, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
    // A-B系统SOC差过大
    t_threshold = 0;
    BMS_INIT_HIGH_POINT(&APP_L3_FAIL_INST.SOC_AB_Diff, RT_FALSE, RT_FALSE, RT_FALSE, t_threshold, ALM_CONFIRM_TIME_5S);
}

static void bms_exception_event_init(void)
{
    STOP_WARNING_SOUND = RT_FALSE;
}

void bms_exception_event_iterate(void)
{
    //
    rt_uint16_t temp_word = 0;
    //
    rt_int16_t compare_value = 0;
    //
    while (1)
    {
        //
        rt_thread_mdelay(ALM_SCAN_TIME_INTERVAL);
    }
}

void bms_exception_event_reset(void)
{
    //
    bms_exception_event_init();
}

int bms_exception_event_thread(void)
{
    rt_thread_t thread_id;
    bms_exception_event_init();

    thread_id = rt_thread_create("alarm", (void (*)(void*))bms_exception_event_iterate, RT_NULL, 1024, 14, 10);
    if (thread_id != RT_NULL)
    {
        rt_thread_startup(thread_id);
    }
    else
    {
        goto __exit;
    }
    return RT_EOK;

__exit:
    if (thread_id)
        rt_thread_delete(thread_id);
    return RT_ERROR;
}